/**
 * @description 
 * @author cq
 * @Date 2021-04-08 10:56:32
 * @LastEditTime 2021-04-08 11:21:02
 * @LastEditors cq
 */

export default function combineReducers(reducers) {
  return (state = {}, action) => {
    let nextState = {};
    let ishas=false;
    for (const key in reducers) {
      const reducer = reducers[key];
      nextState[key] = reducer(state[key], action)
      ishas = ishas || nextState[key] !== state[key]
    }
    // 为了防止state的改变需要区分下长度  比如{a:1,b:2}变成{a:1}
    ishas = ishas || Object.keys(nextState).length !== Object.keys(state).length
    return ishas ? nextState : state
  }
}